<?php
//定时执行地址:http://zhibo3.oumei88.com/admin/tongji/statistics.php
require_once '../../include/common.inc.php';

switch($act) {
	case 'online':
		$today = date('y-m-d');
		$_now = date('H:i');
		$i = date('i');
		//if($i == '30' || $i == '00') {
			$online_num = $_GET['online_num'];
			$sql = "replace into {$tablepre}online_stat(`cdate`, `ctime`, `online_num`) values('$today', '$_now', '$online_num')";
			$db->query($sql);
		//}
		//echo '<script>location.reload();</script>';
		break;
}

?>

<script type="text/javascript" src="../assets/js/jquery-1.8.1.min.js"></script> 
<script type="text/javascript" src="../assets/js/bui.js"></script> 
<script type="text/javascript" src="../assets/js/config.js"></script> 
<script type="text/javascript" src="../assets/js/socket.io.js"></script>
<script type="text/javascript">
	var onlineNum = 0;
	var bool = false;
	var socket;
	$(document).ready(function() {
		//OnSocket();
		setInterval("jishi()", 1000);
	});
	
	function jishi() {
		var now=new Date();
		var shi=now.getHours();
		var fen=now.getMinutes();
		var miao=now.getSeconds();
		if((fen == '30' || fen == '0') && miao == '0') {
			if(!bool) {
				OnSocket();
				bool = true;
			} else {
				var str = 'OnlineUser=M';
				socket.emit('onlineUser', str);
			}
		}
		//document.write('<br>' + shi + ':' + fen + ':' + miao);
	}

	function OnSocket() {    
		socket = io("ws://<?php echo $cfg['config']['tserver']; ?>");
		socket.on('connect', function() {
			var str = 'OnlineUser=M';
			//socket.emit('message', str);
			socket.emit('onlineUser', str);
		});
		socket.on('onlineUser', function(data) {
			console.log(data);
			WriteMessage(data);
		});
	}
	
	function WriteMessage(txt) {
		var Msg;
		try {
			Msg = eval("(" + txt + ")");
		} catch (e) {
			return;
		}
		switch (Msg.type) {				
			case "onlineUser":
				var onlineNum = Msg.roomListUser.length;
				//console.log(onlineNum);
				$.getJSON('/admin/tongji/statistics.php?act=online&online_num=' + (onlineNum - 1), function(data) {
					console.log(data);
				});
				break;
		}
	}
	
	function jump() {
		location.reload();
	}
</script>